﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableNamePickableControl
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Flexibles;
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands;

public class CApCableNamePickableControl : 
  System.Windows.Controls.UserControl,
  ICApFLEXCableEditorSelection,
  IDisposable,
  IComponentConnector
{
  private bool m_isControlLoading;
  private bool m_isBeingFocused;
  private bool m_isBeingPicked;
  private bool m_forceOnlyCableEditorNameChanging;
  private ITxCable m_cableEditorObject;
  private ICApFLEXCableAttributeViewModel m_attributeViewModel;
  internal TxObjEditBoxControl m_editBox;
  private bool _contentLoaded;

  public CApCableNamePickableControl()
  {
    this.InitializeComponent();
    this.m_isControlLoading = true;
    this.m_isBeingFocused = false;
    this.m_isBeingPicked = false;
    this.m_forceOnlyCableEditorNameChanging = false;
  }

  public void Dispose()
  {
    this.m_attributeViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
  }

  public void InitializeControl(ICApFLEXCableAttributeViewModel attributeViewModel)
  {
    this.m_attributeViewModel = attributeViewModel ?? throw new ArgumentNullException(nameof (attributeViewModel));
    this.m_editBox.SetValidator((ITxValidator) new CApFLEXCableValidator((ICApFLEXCableEditorSelection) this));
    this.m_attributeViewModel.PropertyChanged += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
  }

  public void EndOfLoading()
  {
    this.m_isControlLoading = false;
    this.m_cableEditorObject = this.Cable;
    this.m_editBox.Text = this.CableName;
  }

  public event CApCableTxCableEventHandler CableChanged;

  public ITxCable CableEditorObject
  {
    get => this.m_cableEditorObject;
    set
    {
      if (this.m_isBeingFocused)
        return;
      this.m_cableEditorObject = value;
      if (this.m_forceOnlyCableEditorNameChanging)
        return;
      this.UpdateName(this.m_isBeingPicked && this.Cable == null && value == null && this.m_editBox.Text == string.Empty);
    }
  }

  public bool ShouldDisplayCableEditorEmptyToolTip()
  {
    return !this.m_isControlLoading && !this.m_isBeingPicked;
  }

  private void UpdateName(bool shouldRestorePreviousName)
  {
    if (shouldRestorePreviousName && this.m_editBox.Text != this.CableName)
      this.m_editBox.Text = this.CableName;
    else
      this.CableName = this.m_editBox.Text;
  }

  private void EditBox_GotFocus(object sender, RoutedEventArgs e)
  {
    this.m_isBeingFocused = true;
    CApCableUtility.SelectObject((ITxObject) this.Cable);
    this.m_editBox.Text = this.CableName;
    this.m_isBeingFocused = false;
  }

  private void EditBox_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    this.m_isBeingPicked = true;
    if (args.Object is TxCable cable)
    {
      if (this.Cable == null || this.Cable != cable)
      {
        this.m_forceOnlyCableEditorNameChanging = true;
        string text = this.m_editBox.Text;
        this.m_editBox.Text = this.CableName;
        bool flag = true;
        if (this.Cable == null || this.Cable != null && this.m_attributeViewModel.HasParameterChanged())
          flag = TxMessageBox.ShowModal(string.Format(CApFLEXStringTable.EDIT_CABLE_UPDATE_WARNING, (object) this.CableName), CApFLEXStringTable.EDIT_CABLE_UPDATE_TITLE, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK;
        if (flag)
        {
          this.CableName = text;
          this.m_forceOnlyCableEditorNameChanging = false;
          this.UpdateName(true);
          CApCableTxCableEventHandler cableChanged = this.CableChanged;
          if (cableChanged != null)
            cableChanged((ITxCable) cable);
        }
        else
          this.UpdateName(true);
      }
    }
    else if (this.Cable != null)
    {
      this.m_editBox.Object = (ITxObject) this.Cable;
      this.m_editBox.Text = ((ITxObject) this.Cable).Name;
    }
    else
      this.UpdateName(true);
    this.m_isBeingPicked = false;
  }

  private void AttributeViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    if (e.PropertyName == "CableName" && this.m_editBox.Text != this.CableName)
    {
      this.m_editBox.Text = this.CableName;
    }
    else
    {
      if (!(e.PropertyName == "Cable"))
        return;
      this.m_cableEditorObject = this.Cable;
    }
  }

  private ITxCable Cable => this.m_attributeViewModel.Cable;

  private string CableName
  {
    get => this.m_attributeViewModel.CableName;
    set => this.m_attributeViewModel.CableName = value;
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/flexibles/editboxcontrols/capcablenamepickablecontrol.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    if (connectionId == 1)
    {
      this.m_editBox = (TxObjEditBoxControl) target;
      ((UIElement) this.m_editBox).GotFocus += new RoutedEventHandler(this.EditBox_GotFocus);
      this.m_editBox.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.EditBox_Picked);
    }
    else
      this._contentLoaded = true;
  }
}
